package com.ruoyi.system.controller;

import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysDictType;
import com.ruoyi.system.service.ISysOrgDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@Api(tags = "数据字典信息")
@RestController
@RequestMapping("/org/dict/type")
public class SysOrgDictTypeController extends BaseController {
    @Autowired
    private ISysOrgDictTypeService orgDictTypeService;

    @RequiresPermissions("system:orgDict:list")
    @GetMapping("/list")
    @ApiOperation("查询数据字典列表")
    public TableDataInfo list(SysDictType dictType) {
        startPage();
        List<SysDictType> list = orgDictTypeService.selectDictTypeList(dictType);
        return getDataTable(list);
    }

    @PostMapping("/typeList")
    @ApiOperation("查询数据字典列表")
    @InnerAuth
    public AjaxResult typeList(@RequestBody SysDictType dictType) {
        List<SysDictType> list = orgDictTypeService.selectDictTypeList(dictType);
        return success(list);
    }

    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:orgDict:export")
    @PostMapping("/export")
    @ApiOperation("导出数据字典列表")
    public void export(HttpServletResponse response, SysDictType dictType) {
        List<SysDictType> list = orgDictTypeService.selectDictTypeList(dictType);
        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
        util.exportExcel(response, list, "字典类型");
    }

    /**
     * 查询字典类型详细
     */
    @RequiresPermissions("system:orgDict:query")
    @GetMapping(value = "/{dictId}")
    @ApiOperation(value = "查询字典类型详细", response = SysDictType.class)
    public AjaxResult getInfo(@ApiParam(value = "字典类型", required = true) @PathVariable Long dictId) {
        return success(orgDictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("system:orgDict:add")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增字典类型")
    public AjaxResult add(@Validated @RequestBody SysDictType dict) {
        if (UserConstants.NOT_UNIQUE.equals(orgDictTypeService.checkDictTypeUnique(dict))) {
            return error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(orgDictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */
    @RequiresPermissions("system:orgDict:edit")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改字典类型")
    public AjaxResult edit(@Validated @RequestBody SysDictType dict) {
        if (UserConstants.NOT_UNIQUE.equals(orgDictTypeService.checkDictTypeUnique(dict))) {
            return error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(orgDictTypeService.updateDictType(dict));
    }

    /**
     * 删除字典类型
     */
    @RequiresPermissions("system:orgDict:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{dictIds}")
    @ApiOperation(value = "删除字典类型")
    public AjaxResult remove(@ApiParam(value = "字典类型id", required = true) @PathVariable Long[] dictIds) {
        orgDictTypeService.deleteDictTypeByIds(dictIds);
        return success();
    }

    /**
     * 刷新字典缓存
     */
    @RequiresPermissions("system:orgDict:remove")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/delete/refreshCache")
    @ApiOperation("刷新字典缓存")
    public AjaxResult refreshCache() {
        orgDictTypeService.resetDictCache();
        return success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    @ApiOperation(value = "获取字典选择框列表", response = SysDictType.class)
    public AjaxResult optionselect() {
        List<SysDictType> dictTypes = orgDictTypeService.selectDictTypeAll();
        return success(dictTypes);
    }
}
